package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.MesSaleOrderDetail;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.MesSaleOrderDetailDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-30 15:35:52
 */
public class MesSaleOrderDetailConverts {

    private static Logger LOG = LoggerFactory.getLogger(MesSaleOrderDetailConverts.class);

    public static List<MesSaleOrderDetailDTO> convert(List<MesSaleOrderDetail> mesSaleOrderDetailList) {
        if (CollectionUtils.isEmpty(mesSaleOrderDetailList)) {
            return Collections.EMPTY_LIST;
        }

        List<MesSaleOrderDetailDTO> mesSaleOrderDetailListResult = Lists.newArrayList();
        for (MesSaleOrderDetail mesSaleOrderDetail : mesSaleOrderDetailList) {
            mesSaleOrderDetailListResult.add(convert(mesSaleOrderDetail));
        }
        return mesSaleOrderDetailListResult;
    }

    public static MesSaleOrderDetailDTO convert(MesSaleOrderDetail mesSaleOrderDetail) {
        try {
            return BeanUtils.copy(mesSaleOrderDetail, MesSaleOrderDetailDTO.class);
        } catch (Exception e) {
            LOG.error("MesSaleOrderDetail到MesSaleOrderDetailDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesSaleOrderDetail到MesSaleOrderDetailDTO转换失败", e);
        }
    }

    public static MesSaleOrderDetail convert(MesSaleOrderDetailDTO mesSaleOrderDetailDTO) {
        try {
            return BeanUtils.copy(mesSaleOrderDetailDTO, MesSaleOrderDetail.class);
        } catch (Exception e) {
            LOG.error("MesSaleOrderDetailDTO到MesSaleOrderDetail转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "MesSaleOrderDetailDTO到MesSaleOrderDetail转换失败", e);
        }
    }
}